package controller.uu;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import model.dao.AlbumDaoLocal;
import model.dao.UserDaoLocal;
import model.entity.Album;
import model.entity.User;
import controller.CUtil;
import controller.ur.SAuthorFollowRemove;

/**
 * Servlet implementation class SRegisterOut
 */
public class SRegisterOut extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@EJB
	private UserDaoLocal userDao;
	
	@EJB
	private AlbumDaoLocal albumDao;
	
	private Logger log=Logger.getLogger(SRegisterOut.class);
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SRegisterOut() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	request.setCharacterEncoding("UTF-8");
		String email=(String)request.getParameter("email");
		if(userDao.mailExists(email)){
			request.setAttribute("message", "Email already exists");
			CUtil.redirecter(request, response, "SRegister");
			return;
		}
		String password=(String)request.getParameter("password");
		String firstName=(String)request.getParameter("firstName");
		String lastName=(String)request.getParameter("lastName");
		int gender=CUtil.genderToInt((String)request.getParameter("gender"));
		
		String dateOfBirths=(String)request.getParameter("dateOfBirth");
		Date dateOfBirth=null;
		if(!dateOfBirths.equals("")){
			String[] dateComponents=dateOfBirths.split("/");
			Calendar dateOfBirthc=Calendar.getInstance();
			dateOfBirthc.set(Integer.parseInt(dateComponents[2]),Integer.parseInt(dateComponents[1])-1,
					Integer.parseInt(dateComponents[0]));
			dateOfBirth=dateOfBirthc.getTime();
		}
		String[] notifications=request.getParameterValues("notifications");
		boolean notify;
		if(notifications!=null && notifications.length!=0){
			notify=true;
		}else{
			notify=false;
		}
		
		User user=new User();
		user.setEmail(email);
		user.setPassword(password);
		user.setFirstName(firstName);
		user.setLastName(lastName);
		user.setGender(gender);
		user.setDateOfBirth(dateOfBirth);
		user.setEmailNotifications(notify);
		userDao.persist(user);
		
		Album favoriteAlbum=new Album();
		favoriteAlbum.setTitle("fav"+user.getId());
		favoriteAlbum.setFavorite(true);
		user.setFavorite(favoriteAlbum);
		favoriteAlbum.setUser(user);
		albumDao.persist(favoriteAlbum);
		userDao.merge(user);

		log.info("New user registered: "+user.toString());
		
		CUtil.lastPageRedirect(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}
